package com.github.caijh.mall.system.controller;

import java.util.List;

import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.system.model.SystemCity;
import com.github.caijh.mall.system.request.SystemCityListReqBody;
import com.github.caijh.mall.system.request.SystemCityRequest;
import com.github.caijh.mall.system.service.SystemCityService;
import com.github.caijh.mall.system.vo.SystemCityTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/admin/system/city")
@Api(tags = "城市管理")
public class SystemCityController extends BaseController {

    @Autowired
    private SystemCityService systemCityService;

    /**
     * 分页显示城市表
     *
     * @param reqBody 搜索条件
     * @author Mr.Zhang
     * @since 2020-04-17
     */
    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/list")
    public CommonResult<Object> getList(@Validated SystemCityListReqBody reqBody) {
        return CommonResult.success(systemCityService.getList(reqBody));
    }

    /**
     * 修改城市
     *
     * @param id      integer id
     * @param request 修改参数
     * @author Mr.Zhang
     * @since 2020-04-17
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public CommonResult<String> update(@RequestParam Integer id, @Validated SystemCityRequest request) {
        if (systemCityService.update(id, request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改状态
     *
     * @param id     integer id
     * @param status boolean 状态
     * @author Mr.Zhang
     * @since 2020-04-17
     */
    @ApiOperation(value = "修改状态")
    @PostMapping(value = "/update/status")
    public CommonResult<String> updateStatus(@RequestParam Integer id, @RequestParam Boolean status) {
        if (systemCityService.updateStatus(id, status)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询城市表信息
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-17
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    public CommonResult<SystemCity> info(@RequestParam(value = "id") Integer id) {
        SystemCity systemCity = systemCityService.getById(id);
        return CommonResult.success(systemCity);
    }

    /**
     * 获取tree结构的列表
     *
     * @return {@code List<SystemCityTreeVo>}
     */
    @ApiOperation(value = "获取tree结构的列表")
    @GetMapping(value = "/list/tree")
    public List<SystemCityTreeVo> tree() {
        return systemCityService.tree();
    }

}



